#!/usr/bin/env groovy
import static groovy.io.FileType.FILES;




def target = null;

if(!args || args.length < 2){
	println args;
	println "gcp arg1 arg2 (xcp /home/xianwche/Hello*.jar .)"
	return -1;
} else {

	for( int i =0; i <= args.size() -2; i++){
		def temp = new File(args[i]);
		if(!temp.exists()){
			println "Can not find the file ${args[i]}";
			return -1;
		}
	}

	target = new File(args[args.size() -1]);
	if(!target.exists() || target.isFile()){
		println "The target must be a folder";
		return -1;
	} else {
		target = target.getAbsolutePath();
	}
	
}

new File(target).eachFileRecurse(FILES) { it ->
	for( int i =0; i <= args.size() -2; i++){
		def temp = new File(args[i]);
		if(it.name.startsWith(temp.name)) {
			(new AntBuilder( )).copy ( file : temp , tofile : it ,  overwrite: true, failonerror: true, verbose: true)
			break;
		}
	}
}





